package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yz
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/api/edu/teacher")

public class ApiTeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;

    //查询所有讲师
    @GetMapping
    public R  findAll() {
        List<Teacher> teachers = teacherService.list(null);
        return R.ok().data("teachers", teachers).message("获取讲师列表成功");
    }
    //根据id查询讲师
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") String id){
        Teacher teacher = teacherService.getOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getId, id)
                .select(Teacher::getAvatar, Teacher::getLevel, Teacher::getName, Teacher::getCareer,
                        Teacher::getIntro, Teacher::getId));
        return R.ok().data("item",teacher);
    }
    //@ApiOperation("测试服务调用")
    //@GetMapping("/test")
    //public R test(){
    //    ossFileService.test();
    //    return R.ok();
    //}
    //查询首页4个热门讲师
    //@Cacheable(value = "ads",key = "'cache'")
    @GetMapping("/getHotTeachers")
    public R getHotTeachers(){
        List<Teacher> teachers = teacherService
                .list(new LambdaQueryWrapper<Teacher>()
                        .select(Teacher::getName, Teacher::getAvatar,
                                Teacher::getId).orderByDesc(Teacher::getSort).last("limit 4"));
        return R.ok().data("items",teachers);
    }
}

